package software;

import java.io.*;
import java.util.*;

public class Evaluate {
    private static File file;
    public Evaluate(File file){
    	Evaluate.file = file;
    }
    //评价函数
    public static String analyze(){
        FileReader fr = null;
        try {
            fr = new FileReader(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(fr);
        Date time = new Date();
        String s = null;
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (s == null) return "您还没有做题记录,快去练习吧!";
        ArrayList<Date> date = getDate();
        ArrayList<Double> dble = getScore();
        double sum = 0;
        for (int i = 0; i < dble.size(); i++) sum += dble.get(i);
        Date CurTime = new Date(time.getYear() + 1900, time.getMonth() + 1, time.getDate(), time.getHours(), time.getMinutes());
        if ((CurTime.getTime() - date.get(date.size() - 1).getTime()) / (24 * 60 * 60 * 1000) > 1) return "您已经很久没有练习题目了";
        if (date.size() > 5 && (CurTime.getTime() - date.get(date.size() - 5).getTime()) / (24 * 60 * 60 * 1000) <= 1) return "您最近做题比较勤奋";
        if (sum / dble.size() < 80) return "您最近一次做题的准确率偏低，平均低于80%，加油努力";
        if (sum / dble.size() > 90) return "您最近一次做题准确率很高，平均高于90%，继续保持";
        return "您最近一次做题准确率比较高，平均高于80%，继续加油";
    }
  //获取X轴的时间数据
    public static ArrayList<Date> getDate(){
        FileReader fr = null;
        try {
            fr = new FileReader(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(fr);
        ArrayList<Date> t = new ArrayList<>();
        String line = null;
        while (true){
            Date d = new Date();
            try {
                if ((line = br.readLine()) == null) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            String[] split = line.split(",");
            d.setYear(Integer.parseInt(split[0]));
            d.setMonth(Integer.parseInt(split[1]));
            d.setDate(Integer.parseInt(split[2]));
            d.setHours(Integer.parseInt(split[3]));
            d.setMinutes(Integer.parseInt(split[4]));
            t.add(d);
        }
        ArrayList<Date> a = new ArrayList<>();
        int startIdex = t.size() - 50;
        if (startIdex < 0) startIdex = 0;
        for (int i = startIdex; i < t.size(); i++){
            a.add(t.get(i));
        }
        try {
            fr.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return a;
    }
  //获取Y轴分值数据
    public static ArrayList<Double> getScore(){
        FileReader fr = null;
        try {
            fr = new FileReader(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(fr);
        ArrayList<Double> t = new ArrayList<>();
        String line = null;
        while (true){
            try {
                if (!((line = br.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            String[] split = line.split(",");
            t.add(Double.parseDouble(split[5]));
        }
        ArrayList<Double> a = new ArrayList<>();
        int startIdex = t.size() - 50;
        if (startIdex < 0) startIdex = 0;
        for (int i = startIdex; i < t.size(); i++){
            a.add(t.get(i));
        }
        try {
            fr.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return a;
    }
    public static void writeScore(double d){
        Date time = new Date();
        FileWriter fw = null;
        try {
            fw = new FileWriter(file, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedWriter bw = new BufferedWriter(fw);
        try {
            bw.append(Integer.toString(time.getYear() + 1900) + ",");
            bw.append(Integer.toString(time.getMonth() + 1) + ",");
            bw.append(Integer.toString(time.getDate()) + ",");
            bw.append(Integer.toString(time.getHours()) + ",");
            bw.append(Integer.toString(time.getMinutes()) + ",");
            bw.append(Double.toString(d) + ",");
            bw.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
